import { checkIsOwner } from '@/pages/xbommanage/SpecialOrder/utils';

// 转移责任人，转移接口人，转移协助人权限
export const TransferPersonAuths = Object.freeze({
  responsible: {
    authType: 'owner',
    api: '/bommgmt/specialOrder/transferOwner',
    fieldName: 'owner',
    title: '转移责任人',
  },
  interface: {
    authType: 'interface',
    api: '/bommgmt/specialOrder/transferInterfacePerson',
    fieldName: 'interfacePerson',
    title: '转移接口人',
  },
  assist: {
    authType: 'owner',
    api: '/bommgmt/specialOrder/transferHelpPersons',
    fieldName: 'helpPersonsStr',
    title: '设置协助人',
  },
  setmbom: {
    authType: 'owner',
    api: '/bommgmt/specialOrder/saveBatch',
    fieldName: 'authPersonsStr',
    title: '设置MBOM工程师',
  },
});

// 检查是否存在权限
export function checkAuth(type, selectedRowObj) {
  let isOwner = true; // 是否有权限操作
  const normal = {
    btnTip: tr(TransferPersonAuths[type]?.title),
    btnable: true,
  }; // 正常有权限的文案
  switch (type) {
    case 'responsible':
      isOwner = checkIsOwner(selectedRowObj, TransferPersonAuths.responsible.fieldName);
      return isOwner
        ? normal
        : {
            btnTip: '非责任人禁止操作',
            btnable: false,
          };
      break;
    case 'interface':
      isOwner = checkIsOwner(selectedRowObj, TransferPersonAuths.interface.fieldName);
      return isOwner
        ? normal
        : {
            btnTip: '非接口人禁止操作',
            btnable: false,
          };
      break;
    case 'assist':
      isOwner = checkIsOwner(selectedRowObj, TransferPersonAuths.responsible.fieldName);
      return isOwner
        ? normal
        : {
            btnTip: '非责任人禁止操作',
            btnable: false,
          };
      break;
    case 'setmbom':
      isOwner = checkIsOwner(selectedRowObj, TransferPersonAuths.responsible.fieldName);
      return isOwner
        ? normal
        : {
            btnTip: '非责任人禁止操作',
            btnable: false,
          };
      break;
    default:
      return normal;
      break;
  }
}

// 获取接口参数
export const getExtraParams = (type, selectedRowObj, ids: any[]) => {
  switch (type) {
    case 'interface':
      return {
        interfacePerson: ids,
        id: selectedRowObj?.id,
      };
      break;
    case 'responsible':
      return {
        owner: ids,
        id: selectedRowObj?.id,
      };
      break;
    case 'assist':
      return {
        helpPersons: ids.map((item) => {
          return {
            helpPersonId: item,
          };
        }),
        id: selectedRowObj?.id,
      };
      break;
    case 'setmbom':
      return {
        authPersonIds: ids,
        orderId: selectedRowObj?.id,
      };
      break;
    default:
      return {};
  }
};
